package com.android.sharecar.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.sharecar.model.ReservationPickRequestMsg;
import com.android.sharecar.model.User;
import com.android.sharecar.util.BitmapUtil;
import com.google.android.maps.GeoPoint;

/**
 * This activity display the reserved pick ride request list... 
 * @author xinyan
 *@date 2011-10-13
 */
public class ReservePickRequestListActivity extends ListActivity {
	public static final String TAG = "ReserveRequestListActivity";
	// public static final String

	// Members
	private static List<ReservationPickRequestMsg> mRequestMsgs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_reserve_pick_request);

		searchUsersFromServerByGeoPoint(null);
		setListAdapter(new RequestListAdapter(this,
				R.layout.list_item_reserve_request, mRequestMsgs));
		setSelection(6);
	}

	private void searchUsersFromServerByGeoPoint(GeoPoint point) {
		mRequestMsgs = new ArrayList<ReservationPickRequestMsg>();
		User user;
		ReservationPickRequestMsg msg;
		Bitmap headPic;
		BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources()
				.getDrawable(R.drawable.myheadpic);
		headPic = bitmapDrawable.getBitmap();

		for (int i = 0; i < 8; i++) {
			user = new User();
			msg = new ReservationPickRequestMsg();

			user.setNickName("用户" + i);
			user.setGender("男");
			user.setHeadPicture(BitmapUtil.getBytes(headPic));

			msg.setUser(user);
			msg.setStartPlace("二拨子");
			msg.setEndPlace("天安门");
			msg.setReservationTime(new Date());
			msg.setSeats(1);

			mRequestMsgs.add(msg);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		intent.setClass(this, ReservePickRequestDetailActivity.class);
		startActivity(intent);
	}

	private final class RequestListAdapter extends BaseAdapter {
		private final Context mContext;
		private final Integer mResource;
		private final List<ReservationPickRequestMsg> mData;

		public RequestListAdapter(Context context, Integer resource,
				List<ReservationPickRequestMsg> value) {
			this.mContext = context;
			this.mResource = resource;
			this.mData = value;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(TAG, "getView-----" + position + "---convertView--"
					+ convertView);
			TextView startPlace, endPlace, reservationTime, seats;
			View view = View.inflate(mContext, mResource, null);
			ImageView headPic = (ImageView) view
					.findViewById(R.id.reserveRequestList_iv_headPic);
			/*	nickName = (TextView) view
						.findViewById(R.id.reserveRequestList_tv_nickName);
				gender = (TextView) view
						.findViewById(R.id.reserveRequestList_tv_gender);*/
			startPlace = (TextView) view
					.findViewById(R.id.reserveRequestList_tv_startPlace);
			endPlace = (TextView) view
					.findViewById(R.id.reserveRequestList_tv_endPlace);
			reservationTime = (TextView) view
					.findViewById(R.id.reserveRequestList_tv_reserveTime);
			seats = (TextView) view
					.findViewById(R.id.reserveRequestList_tv_seats);

			ReservationPickRequestMsg msg = mData.get(position);
			if (null != msg) {
				User user = msg.getUser();
				if (null != user) {
					headPic.setImageBitmap(BitmapUtil.getBitmap(user
							.getHeadPicture()));
					/*nickName.setText(user.getNickName());
					gender.setText(user.getGender());*/
				}
				startPlace.setText(msg.getStartPlace());
				endPlace.setText(msg.getEndPlace());
				reservationTime.setText(msg.getReservationTime()
						.toLocaleString());
				seats.setText(String.valueOf(msg.getSeats()));
			}

			return view;
		}

	}
}
